package com.dreamwhale.machinery.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.user.entity.YxUserBill;
import com.dreamwhale.machinery.user.mapper.YxUserBillMapper;
import com.dreamwhale.machinery.user.mapper.YxUserMapper;
import com.dreamwhale.machinery.user.service.YxUserBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户账单表 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-27
 */
@Service
public class YxUserBillServiceImpl extends ServiceImpl<YxUserBillMapper, YxUserBill> implements YxUserBillService {
    @Resource
    private YxUserBillMapper yxUserBillMapper;

    @Override
    public DataResult getUserBill(Integer user, Integer page) {
        PageHelper.startPage(page, 50);
        List<YxUserBill> yxUserBills = yxUserBillMapper.selectList(new QueryWrapper<YxUserBill>().eq("uid", user).orderByDesc("add_time"));
        if (CollectionUtils.isEmpty(yxUserBills)) {
            return new DataResult(300, "未有交易记录", new PageInfo<>(yxUserBills));
        }
        return new DataResult(200, "查询成功", new PageInfo<>(yxUserBills));
    }

    @Override
    public int insertYxUserBills(YxUserBill yxUserBill) {
        yxUserBill.setAddTime(LocalDateTime.now());
        return yxUserBillMapper.insert(yxUserBill);
    }
}
